package com.lindroy.androidplayer.ui.home.fragment

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.tabs.TabLayout
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.extensions.no
import com.lindroy.androidplayer.extensions.parseState
import com.lindroy.androidplayer.extensions.setNetworkErrorLayout
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.logic.model.WXChapterResp
import com.lindroy.androidplayer.ui.adapter.BaseTabAdapter
import com.lindroy.androidplayer.ui.adapter.WXTabAdapter
import com.lindroy.androidplayer.viewmodel.WXArticleViewModel
import com.lindroy.lib_utils.extensions.setGone
import com.lindroy.lib_utils.extensions.setVisible
import kotlinx.android.synthetic.main.fragment_official_account.*
import kotlinx.android.synthetic.main.tablayout.*

/**
 * @author Lin
 * @date 2021/4/8
 * @function 首页-公众号
 * @Description
 */
class OfficialAccountFragment(override val layoutId: Int = R.layout.fragment_official_account) :
    BaseLazyFragment() {

    private val wxViewModel: WXArticleViewModel by lazy { ViewModelProvider(this)[WXArticleViewModel::class.java] }

    private val chapterList = mutableListOf<WXChapterResp>()

    private val tabAdapter: WXTabAdapter by lazy {
        WXTabAdapter(
            chapterList.map { BaseTabAdapter.TabBean(it.id, it.name) },
            childFragmentManager
        )
    }

    override fun initView() {
        tabLayout.setupWithViewPager(vpArticle)
        addLoadingObserve(wxViewModel)
    }

    override fun lazyLoad() {
        wxViewModel.reqWXChapters()
    }

    override fun initListener() {
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let { vpArticle.currentItem = it.position }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
        wxViewModel.wxChapters.observe(this) { resultState ->
            parseState(resultState, onError = {
                tabLayout.setGone()
                stateLayout.setNetworkErrorLayout()
            }) {
                it.isNotEmpty().yes {
                    tabLayout.setVisible()
                    vpArticle.offscreenPageLimit = it.size
                    stateLayout.showContent()
                    chapterList.addAll(it)
                    vpArticle.adapter = tabAdapter
                }.no {
                    tabLayout.setGone()
                    stateLayout.showEmpty()
                }
            }
        }
    }

    override fun initClick() {
        stateLayout.setOnViewsClickListener { state, view ->
            wxViewModel.reqWXChapters()
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            OfficialAccountFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }
}